package day06;

import org.springframework.jdbc.core.JdbcTemplate;
import work.JDBCUtils1;

public class Demo2 {
    public static void main(String[] args) throws Exception {
        String sql = "create table student(id int primary key auto_increment,name varchar(20) not null,birthday date)";
        //1. 创建JdbcTemplate对象，传入C3P0连接池
        JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils1.getDataSource());
        //2. 创建表，执行DDL语句
        jdbcTemplate.execute(sql);
    }
}
class Demo3 {

    private static JdbcTemplate jdbcTemplate;

    static {
        try {
            jdbcTemplate = new JdbcTemplate(JDBCUtils1.getDataSource());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        insert();
//        update();
//        delete();
    }

    //添加记录
    public static void insert() {
        jdbcTemplate.update("insert into student values (null, ?,?)", "孙悟空","1999-12-10");
        jdbcTemplate.update("insert into student values (null, ?,?)", "孙悟天","1999-12-10");
        jdbcTemplate.update("insert into student values (null, ?,?)", "孙悟饭","1999-12-10");
        jdbcTemplate.update("insert into student values (null, ?,?)", "龟仙人","1999-12-10");
        jdbcTemplate.update("insert into student values (null, ?,?)", "牛魔王","1999-12-10");
        jdbcTemplate.update("insert into student values (null, ?,?)", "18号","1997-12-10");
    }

    //修改记录
    public static void update() {
        int row = jdbcTemplate.update("update student set name=?, birthday=? where id=?", "库林", "1994-12-10", 2);
        System.out.println("更新了" + row + "行记录");
    }
    //删除记录
    public static void delete() {
        int row = jdbcTemplate.update("delete from student where id=?", 6);
        System.out.println("删除" + row + "行记录");
    }
}
